﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OOLib
{
   /// <summary>
   /// Component context to be passed to a component via ::com::sun::star::lang::XSingleComponentFactory. 
   /// Arbitrary values (e.g. deployment values) can be retrieved from the context.
   /// </summary>
   public class ComponentContext : ComObjectWrapper, IComponentContext
   {
      #region Constructors

      /// <summary>
      /// Constructor
      /// </summary>
      /// <remarks>
      /// After using this constructor, the ComObj must be set manually!
      /// </remarks>
      public ComponentContext() :
         base()
      {
         _ComponentContextImpl = new ComponentContextImpl(this);
      }

      /// <summary>
      /// Constructor
      /// </summary>
      /// <param name="comObject">COM object</param>
      public ComponentContext(object comObject) :
         base(comObject)
      {
         _ComponentContextImpl = new ComponentContextImpl(this);
      }

      #endregion

      #region IComponentContext

      private ComponentContextImpl _ComponentContextImpl;

      /// <summary>
      /// Gets a value from the context.
      /// </summary>
      /// <param name="name">Name of value</param>
      /// <returns>Returns the value.</returns>
      public object GetValueByName(string name)
      {
         return _ComponentContextImpl.GetValueByName(name);
      }

      /// <summary>
      /// Gets the service manager instance to be used from key /singletons/com.sun.star.lang.theServiceManager (maybe null ref). 
      /// This method has been added for convenience, because the service manager is used very often. 
      /// </summary>
      public MultiComponentFactory ServiceManager 
      { 
         get
         {
            return _ComponentContextImpl.ServiceManager;
         }
      }

      #endregion
   }

   /// <summary>
   /// Component context to be passed to a component via ::com::sun::star::lang::XSingleComponentFactory. 
   /// Arbitrary values (e.g. deployment values) can be retrieved from the context.
   /// </summary>
   public interface IComponentContext
   {
      /// <summary>
      /// Gets a value from the context.
      /// </summary>
      /// <param name="name">Name of value</param>
      /// <returns>Returns the value.</returns>
      object GetValueByName(string name);

      /// <summary>
      /// Gets the service manager instance to be used from key /singletons/com.sun.star.lang.theServiceManager (maybe null ref). 
      /// This method has been added for convenience, because the service manager is used very often. 
      /// </summary>
      MultiComponentFactory ServiceManager { get; }
   }

   /// <summary>
   /// Implementation of the IComponentContext interface
   /// </summary>
   internal class ComponentContextImpl : IComponentContext
   {
      /// <summary>
      /// Constructor
      /// </summary>
      /// <param name="cow">COM object which should support the interface.</param>
      public ComponentContextImpl(ComObjectWrapper cow)
      {
         Cow = cow;
      }

      /// <summary>
      /// Gets the COM object wrapper which should support the interface.
      /// </summary>
      public ComObjectWrapper Cow { get; private set; }

      /// <summary>
      /// Gets a value from the context.
      /// </summary>
      /// <param name="name">Name of value</param>
      /// <returns>Returns the value.</returns>
      /// <remarks>!!! Not tested !!!</remarks>
      public object GetValueByName(string name)
      {
         Cow.CheckDisposed();
         return Cow.InvokeMethod("getValueByName", name);
      } 

      /// <summary>
      /// Gets the service manager instance to be used from key /singletons/com.sun.star.lang.theServiceManager (maybe null ref). 
      /// This method has been added for convenience, because the service manager is used very often. 
      /// </summary>
      /// <remarks>!!! Not tested !!!</remarks>
      public MultiComponentFactory ServiceManager 
      { 
         get
         {
            Cow.CheckDisposed();
            return ComObjectWrapper.GetObjectWrapper<MultiComponentFactory>(Cow.InvokeMethod("getServiceManager"));
         }
      }
   }
}
